package com.sky.service.impl;

import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ReportServiceimpl implements ReportService {
    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {

        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer>orderCountList=new ArrayList<>();
        List<Integer> validOrderCountList=new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime localDateTime1 = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer orderCount = getOrderCount(localDateTime, localDateTime1, null);
            Integer orderCount1 = getOrderCount(localDateTime, localDateTime1, 1);
            orderCountList.add(orderCount);
            validOrderCountList.add(orderCount1);

        }
        log.info("{},{}",orderCountList,validOrderCountList);
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate=0.0;
        if(totalOrderCount!=0) {
        orderCompletionRate=validOrderCount.doubleValue() / totalOrderCount.doubleValue();
        }
        return OrderReportVO.builder().dateList(StringUtils.join(dateList,",")).validOrderCount(validOrderCount).
                orderCountList(StringUtils.join(orderCountList,",")).validOrderCountList(StringUtils.join(validOrderCountList,",")).
                orderCompletionRate(orderCompletionRate).totalOrderCount(totalOrderCount).build();
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dataList =new ArrayList<>();
        dataList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dataList.add(begin);
        }
        List<Integer> newUserList=new ArrayList<>();
        List<Integer> totalUserList=new ArrayList<>();


        for (LocalDate localDate : dataList) {
            LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime localDateTime1 = localDateTime.of(localDate, LocalTime.MAX);
            Map map=new HashMap();
            map.put("end",localDateTime1);
            Integer totalUser  =userMapper.countByMap(map);
            map.put("begin",localDateTime);
            totalUserList.add(totalUser);
            Integer addUser =userMapper.countByMap(map);
            newUserList.add(addUser);
        }
        return UserReportVO.builder().dateList(StringUtils.join(dataList,",")).newUserList(StringUtils.join(newUserList,',')).totalUserList(StringUtils.join(totalUserList,',')).build();
    }
    private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status){
        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);
        return orderMapper.countByMap(map);

    }

    @Autowired
    private OrderMapper orderMapper;
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        List<Double> turnoverList=new ArrayList<>();
        dateList.add(begin);
        while(!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate Date : dateList) {
            LocalDateTime localDateTime = LocalDateTime.of(Date, LocalTime.MIN);
            LocalDateTime localDateTime1 = LocalDateTime.of(Date, LocalTime.MAX);
            Map<String,Object> map=new HashMap<>();
            map.put("begin",localDateTime);
            map.put("end",localDateTime1);
            map.put("status",1);
           Double turnover= orderMapper.sumMap(map);
           if(turnover==null){
               turnover=0.0;
           }
           turnoverList.add(turnover);
        }
        log.info("营业额数据：{},{}",dateList,turnoverList);
        return TurnoverReportVO.builder().dateList(StringUtils.join(dateList,",")).turnoverList(StringUtils.join(turnoverList,',')).build();
    }
}
